#include "head.h"

int readConf(const char* conf,char* ip,char* port,char* threadNum)
{   
    int fd = open(conf,O_RDONLY);
    ERROR_CHECK(fd,-1,"open");

    char tmp[1024] = {0};
    read(fd,tmp,sizeof(tmp));
    
    char* ptmp = strtok(tmp,"\n");
    strcpy(ip,ptmp);
    printf("%s\n",ip);
    ptmp = strtok(NULL,"\n");
    strcpy(port,ptmp);
    printf("%s\n",port);
    ptmp = strtok(NULL,"\n");
    strcpy(threadNum,ptmp);
    printf("%s\n",threadNum);

    return 0;
}

int recvOrder(int netfd,int* orderflag,char* args)
{
    int length;
    char buf[1000];
    recv(netfd,&length,sizeof(int),MSG_WAITALL);
    recv(netfd,buf,length,MSG_WAITALL);
    sscanf(buf,"%d%s",orderflag,args);
    return 0;
}
int pwdSend(int netfd,char*path)
{
    int length;
    length=strlen(path);
    send(netfd,&length,sizeof(int),MSG_WAITALL);
    send(netfd,path,length,MSG_WAITALL);
    return 0;
}

int mkdircreat(char *path)
{
    mkdir(path,0777);
}
int lsSend(int netfd,char* path){
         DIR *pdir=opendir(path);                                                                                               
         ERROR_CHECK(pdir,(void*)0,"opendir");
         struct dirent *pdirent;
         char buff[1024]={0};
         while((pdirent=readdir(pdir))!=NULL)
         {
             const char *name=pdirent->d_name;
             printf("%s\n",name);
             strcpy(buff,name);
             int length;
             length=strlen(name);
             send(netfd,&length,sizeof(int),MSG_NOSIGNAL);
             send(netfd,buff,length,MSG_NOSIGNAL);
        }
        int length=0;
        send(netfd,&length,sizeof(int),MSG_NOSIGNAL);
        closedir(pdir);
        return 0;
}

int checkLogin(int sockfd)
{
    int length;
    char username[256] = {0};
    char userpasswd[256] = {0}; 
    recv(sockfd,&length,sizeof(int),MSG_WAITALL);
    recv(sockfd,username,length,MSG_WAITALL);

    recv(sockfd,&length,sizeof(int),MSG_WAITALL);
    recv(sockfd,userpasswd,length,MSG_WAITALL);
    
    printf("username = %s\n",username);
    printf("userpasswd = %s\n",userpasswd);

    struct spwd* passwd = getspnam(username);
    if(passwd == NULL)
    {
        printf("getspnam: Permission denied!\n");
    }

    char salt[32] = {0};
    strncpy(salt,passwd->sp_pwdp,12);
    
    char* encryptedPasswd =  crypt(userpasswd,salt);
    printf("passwd = %s\n",encryptedPasswd);
    if(strcmp(passwd->sp_pwdp,encryptedPasswd) == 0)
    {
        printf("Login!\n");
        send(sockfd,"1",sizeof(char),MSG_NOSIGNAL);
    }
    else
    {
        printf("Can not login!\n");
        send(sockfd,"2",sizeof(char),MSG_NOSIGNAL);
        return -1;
    }

    return 0;
}


int gets_server(int netfd,char * filename,char * curPath){
    printf("server has started gets_server!\n");
    train_t train;
    train.length = strlen(filename);
    strcpy(train.data,filename);

    send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);

    printf("server has sended the filename!\n");

    char path[1024]={0};
    sprintf(path,"./%s/%s",curPath,filename);
    int fd = open(path,O_RDWR);

    printf("server has opened the file");
    while(1){
        bzero(&train,sizeof(train));

        ssize_t ret = read(fd,train.data,sizeof(train.data));
        train.length = ret;
        send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
        if(ret==0){
            break;
        }
    }

    printf("server has sent the whole file!\n");
    close(fd);
    return 0;
}

int gets_server2(int netfd,char*filename,char *curPath){
    printf("server has started gets_server!\n");
    
    send(netfd,"1",sizeof(char),MSG_NOSIGNAL);
    printf("shake hand");
    int curSize;
    recv(netfd,&curSize,sizeof(int),MSG_WAITALL);
    
    printf("curSize = %d\n",curSize);

    char path[1024]={0};
    sprintf(path,"./%s/%s",curPath,filename);

    int fd = open(path,O_RDWR);

    printf("server has opened the file\n");

    lseek(fd,curSize,SEEK_SET);
    printf("server has lseeked!\n");

    train_t train;

    while(1){
        bzero(&train,sizeof(train));

        ssize_t ret = read(fd,train.data,sizeof(train));

        train.length=ret;
        send(netfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);
        if(ret == 0){
            break;
        }
    }
    printf("server has sent the whole file!\n");
    close(fd);

}
